define(['knockout', 'util', 'config'], function (ko, Util, Config) {
    function ViewModel(params) {
        $.extend(this, {
            striped: true,
            rowHeight: 'auto',
            showHeader: true,
            _css: {},
            _rowStyle: {}
        });
        Util.extend(this, params);

        var scope = this;

        if (scope.showOperation == undefined) scope.showOperation = true;

        scope._css['table-striped'] = scope.striped;

        $.extend(scope._css, Util.generateBindCss(scope.css));


        if (scope.rowHeight != 'auto') scope._rowStyle['height'] = '{0}px'.formatTpl(scope.rowHeight);

        scope._displayFields = ko.observableArray();
        scope._operationType = Config.operationType;

        if (scope.displayFields && scope.displayFields.length > 0) {
            function initDisplayFields(mInstance) {
                var tempFields = [];
                var tempModel = mInstance;
                scope.displayFields.forEach(function (field) {
                    var tempField = {
                        _css: {},
                        _event: {},
                        _columnCmp: {name: 'cmp:Column'},
                        _attr: {}
                    };

                    var modelField = tempModel.getField(field.name);

                    if (field instanceof Object) {
                        tempField.name = field.name;

                        if(field.alias){
                            tempField.alias = field.alias;
                        }
                        else {
                            tempField.alias = modelField?modelField.alias:'';
                        }

                        if (field.click) {
                            tempField._css['xgx-grid-cell-click'] = true;
                        }

                        if (scope.onCellClick) {
                            tempField._event.click = function (data) {
                                scope.onCellClick(/*{field:,record:}*/data);
                            };
                        }

                        if (field.columnCmp) {
                            if (typeof field.columnCmp == 'string') {
                                tempField._columnCmp = {name: field.columnCmp, params: {}};
                            } else {
                                tempField._columnCmp = field.columnCmp;
                            }
                        }

                        if (field.width) {
                            switch (typeof field.width) {
                                case 'number':
                                    tempField._attr['width'] = '{0}px'.formatTpl(field.width);
                                    break;
                                default:
                                    tempField._attr['width'] = field.width;

                            }
                        }

                        if (field.align) {
                            tempField._attr['align'] = field.align;
                        }
                    } else {
                        tempField.name = field;
                        tempField.alias = modelField?modelField.alias:'';
                    }

                    tempFields.push(tempField);
                });

                scope._displayFields(tempFields);
            }


            if (typeof scope.store.model == 'object') {
                require([scope.store.model.name], function (model) {
                    //we only need the field alias so skip the field init logic
                    initDisplayFields(new model({skipInit: true}));
                    //var tempModel = new model({skipInit:true});
                });
            } else {
                initDisplayFields(scope.store._modelInstance);
            }
        }

        if (!scope.preOperation) scope.preOperation = function () {
            return true
        };
        if (!scope.onOperation) scope.onOperation = function () {
        };

        scope._operations = Util.generateOperations(scope.operations);
        scope._onOperation = function (operation, record) {
            if (scope.preOperation(operation)) {
                if (scope.store) {
                    Util.operationSwitch(operation, record);
                }

                scope.onOperation(operation, record);
            }
        };

        scope._calculateOperation = function (record) {
            if (scope.operationFilter) {
                return scope.operationFilter(scope._operations, record);
            }

            return scope._operations;
        };

        scope._calculateTrCss = function (record) {
            if (scope.calculateTrCss) {
                return scope.calculateTrCss(record);
            }

            return {};
        }
    }

    return ViewModel;
});
